<template>
  <div style="display: inline-block">
    <el-upload
      style="display: inline-block"
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="uploadImg"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <span v-else>上传图片</span>
      <i class="el-icon-circle-close"></i>
    </el-upload>
  </div>
</template>
<script>
const COS = require('cos-js-sdk-v5')

export default {
  props: {
    changeList: {
      type: Array
    }
  },
  name: '',
  data () {
    return {
      code: '',
      imageUrl: '',
      percent: 0, // 上传进度
      showProgress: false // 是否展示进度条
    }
  },
  created () {
    console.log(this.code)
  },
  computed: {},
  methods: {
    uploadImg ({ file }) {
      if (file) {
        this.showProgress = true

        // 填写自己腾讯云cos中的key和id (密钥)
        const cos = new COS({
          SecretId: 'AKID1qQjna6ML2SbS2LOALGuIPFo79v9D2bu', // 身份识别ID
          SecretKey: '4tToukKeifVCg28yZtzpPilX2GEvLUMy' // 身份秘钥
        })
        // 执行上传操作
        cos.putObject(
          {
            Bucket: 'yaoyao-1309634971' /* 存储桶 */,
            Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
            Key: file.name /* 文件名 */,
            StorageClass: 'STANDARD', // 上传模式, 标准模式
            Body: file, // 上传文件对象,
            onProgress: (progressData) => {
              console.log(JSON.stringify(progressData))
              this.percent = progressData.percent * 100
            }
          },
          (err, data) => {
            console.log(data, 999000)
            console.log(err || data)
            // 上传成功之后
            if (data.statusCode === 200) {
              this.imageUrl = `https://${data.Location}`
            }
          }
        )
      }
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .avatar-uploader {
  //   position: absolute;
  //   top: 0;
  display: inline-block;
  //   margin-left: 40px;
  //   margin-top: -58px;
  vertical-align: middle;
  position: relative;
}
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed rgb(217, 217, 217);
  // border-radius: 6px;
  cursor: pointer;
  position: relative;
  width: 100px;
  height: 60px;
  line-height: 60px;
  i {
    position: absolute;
    right: 0px;
    top: 0px;
    transform: translate(50%, -50%);
    background: rgb(255, 255, 255);
    font-size: 18px;
    color: rgb(153, 153, 153);
  }
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
span {
  // font-size: 28px;
  //   position: absolute;
  display: inline-block;
  color: #8c939d;
  width: 100px;
  height: 60px;
  line-height: 60px;
  text-align: center;
  // top: 10px;
}
.avatar {
  width: 100px;
  height: 60px;
  display: inline-block;
}
</style>
